package com.ct.ecommerce.idgen.core;

import java.io.Serializable;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicLong;
import lombok.Data;


/**
 * @author changtong
 * @date 2021-50-20
 */
@Data
public class Segment implements Serializable {

    private AtomicLong value = new AtomicLong(0);
    private volatile long max;
    private volatile int step;
    private SegmentBuffer buffer;

    public Segment(SegmentBuffer buffer) {
        this.buffer = buffer;
    }

    public long getIdle() {
        return this.getMax() - getValue().get();
    }

    @Override
    public String toString() {
        StringJoiner s = new StringJoiner("", "Segment{", "}");
        return s.add("value:").add(value.toString()).add(",max:")
            .add(String.valueOf(max)).add(",step:").add(String.valueOf(step))
            .toString();
    }
}